package com.heeexy.example.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.heeexy.example.dao.ClientDao;
import com.heeexy.example.service.ClientService;
import com.heeexy.example.util.CommonUtil;
import com.heeexy.example.util.ExcelUtil;

@RestController
@RequestMapping("/client")
public class ClientController {
	@Autowired
	private ClientService clientService;

	@Autowired
	private ClientDao clientDao;

	@RequiresPermissions("client:list")
	@GetMapping("/list")
	public JSONObject list(HttpServletRequest request) {
		return clientService.listClient(CommonUtil.request2Json(request));
	}

	@RequiresPermissions("client:list")
	@GetMapping("/list/export")
	public void excelDownload(HttpServletRequest request, HttpServletResponse response) throws IOException {

		List<JSONObject> objects = clientDao.listClient(CommonUtil.request2Json(request));

		// 存储全部sheet
		List<List<List<String>>> excelDatas = new ArrayList<>();

		// 表名list
		List<String> sheetNames = new ArrayList<>();

		// 课程信息部分，第一个sheet表格的数据
		List<List<String>> courseData = new ArrayList<>();

		// 定义表头list
		List<String> head = new ArrayList<>();

		// 表头，第一行数据
		head.add("id");
		head.add("姓名");
		head.add("地址");
		head.add("电话");
		head.add("日期");
		

		// 将表头添加到第一个sheet中
		courseData.add(head);

		for (JSONObject object : objects) {
			courseData.add(new ArrayList<>(
					Arrays.asList(object.getString("cid"), object.getString("cname"),object.getString("caddress"),object.getString("ctel"), object.getString("cdate"))));
		}

		// sheet命名
		String courseSheetName = "客户表";

		// 将sheet 表放入全部sheet的list
		excelDatas.add(courseData);
		// 将sheet的表名放入表名list
		sheetNames.add(courseSheetName);
		// 文件名
		String fileName = "course.xls";
		// 导出
		ExcelUtil.exportExcel(response, excelDatas, sheetNames, fileName, 15);
	}

	@RequiresPermissions("client:add")
	@PostMapping("/addClient")
	public JSONObject addClient(@RequestBody JSONObject requestJson) {
		CommonUtil.hasAllRequired(requestJson, "cid,cname,caddress,ctel,cdate");
		return clientService.addClient(requestJson);
	}

	/**
	 * 修改商品
	 */
	@RequiresPermissions("client:update")
	@PostMapping("/updateClient")
	public JSONObject updateClient(@RequestBody JSONObject requestJson) {
		CommonUtil.hasAllRequired(requestJson, "cid,cname,caddress,ctel,cdate");
		return clientService.updateClient(requestJson);
	}

	// 删除商品
	@RequiresPermissions("client:delete")
	@PostMapping("/deleteClient")
	public JSONObject deleteRole(@RequestBody JSONObject requestJson) {
		CommonUtil.hasAllRequired(requestJson, "cid");
		return clientService.deleteClient(requestJson);
	}
}
